package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.article.ArticleTypeModel;
import com.park.client.service.article.ArticleTypeService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/articleType")
public class AdminArticleTypeController extends BaseController{
	
	@Autowired
	private ArticleTypeService articleTypeService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<ArticleTypeModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<ArticleTypeModel>  pageData = new PageData<>();
		ArticleTypeModel articleType = new ArticleTypeModel();
		Integer total = articleTypeService.selectTotal(articleType);
		Page page = new Page(pageNow,total,rows);
		articleType.setStartPos(page.getStartPos());
		articleType.setPageSize(page.getPageSize());
		List<ArticleTypeModel> list = articleTypeService.selectPagination(articleType);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("listTypes")
	@ResponseBody
	public List<ArticleTypeModel> listTypes(HttpServletRequest request){
		return articleTypeService.selectAll();
	}
	
	@RequestMapping("articleTypeList")
	public String articleTypeList(HttpServletRequest request){
			return Global.ADMIN_PATH+"articleType/articleTypeList";
	}
	
	/**
	 *单个文章详情 
	 */
	@RequestMapping("detail")
	public String detail(HttpServletRequest request){
		return "jsp/articleType/articleTypeDetail";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result<Boolean> save(ArticleTypeModel articleType,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		boolean success = articleTypeService.insertSelective(articleType);
		if(success){
			json.setMessage("添加成功!");
		}else{
			json.setMessage("添加失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<Boolean> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		boolean success = articleTypeService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
		}else{
			json.setMessage("删除失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<Boolean> edit(ArticleTypeModel articleType,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		boolean success = articleTypeService.updateByPrimaryKeySelective(articleType);
		if(success){
			json.setMessage("修改成功!");
		}else{
			json.setMessage("修改失败!");
		}
		json.setSuccess(success);
		return json;
	}
}
